#include "GameState.h"

map<string, GameState*> GameState::GameStates;

GameState * GameState::ActiveState = NULL;

GameState * GameState::SwitchToState = NULL;

void GameState::Register( string name, GameState *gstate )
{
    GameStates[name] = gstate;
}

void GameState::Switch( string name )
{
    GameState* state = GameStates[name];
    if (state)
    {
        // TODO
        if (!SwitchToState) SwitchToState = state;
    }else{
        throw std::exception((string("Unknown gamestate: ") + name).c_str());
    }
}

void GameState::SetParam(string name, string value )
{
    m_Params[name] = value;
}

GameState * GameState::Find( string name )
{
    return GameStates[name];
}

string GameState::GetParam( string name )
{
    return m_Params[name];
}

GameState* GameState::GetActiveState()
{
    return ActiveState;
}

void GameState::Update()
{
    if (SwitchToState)
    {                   
        if (ActiveState) ActiveState->Deinitialize();
        GameEngine::ClearObjects();
        ActiveState = SwitchToState;
        SwitchToState = NULL;
        ActiveState->Initialize();        
    }
}



